﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Com.Rivyer.Web.monitor
{
    public partial class monitoroverview : System.Web.UI.Page
    {
        private readonly BLL.monitorBll monitorbll = new BLL.monitorBll();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            if (e.NewValues["department"] == null
                || e.NewValues["department"].ToString().Trim().Length == 0
                || e.NewValues["num"] == null
                || !Maticsoft.Common.PageValidate.IsNumber(e.NewValues["num"].ToString()))
            {
                e.Cancel = true;

            }
            else
            {
                if (e.NewValues["ip"] != null
                    && Maticsoft.Common.PageValidate.IsIp(e.NewValues["ip"].ToString()))
                {
                    //更新IP表数据
                    Model.ipaddressModel ipmodel = new Model.ipaddressModel();
                    ipmodel.ip = e.NewValues["ip"].ToString();
                    ipmodel.department = e.NewValues["department"].ToString();
                    ipmodel.modifydate = DateTime.Now;
                    ipmodel.usedperson = e.NewValues["address"] == null ? ipmodel.department : e.NewValues["address"].ToString();
                    ipmodel.description = "监控";
                    ipmodel.usedinternet = 1;
                    Model.ipaddressModel tmpmodel = new BLL.ipaddressBll().GetModel(int.Parse(new BLL.ipaddressBll().GetList(1, string.Format("ip = '{0}'", ipmodel.ip), "id").Tables[0].Rows[0]["id"].ToString()));
                    ipmodel.id = tmpmodel.id;
                    new BLL.ipaddressBll().Update(ipmodel);
                }
            }
        }

        protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            if (e.NewValues["department"] == null
                || e.NewValues["department"].ToString().Trim().Length == 0
                || e.NewValues["num"] == null
                || !Maticsoft.Common.PageValidate.IsNumber(e.NewValues["num"].ToString()))
            {
                e.Cancel = true;


            }
            else
            {
                if (e.NewValues["ip"] != null
                        && Maticsoft.Common.PageValidate.IsIp(e.NewValues["ip"].ToString()))
                {
                    //更新IP表数据
                    Model.ipaddressModel ipmodel = new Model.ipaddressModel();
                    ipmodel.ip = e.NewValues["ip"].ToString();
                    ipmodel.department = e.NewValues["department"].ToString();
                    ipmodel.modifydate = DateTime.Now;
                    ipmodel.usedperson = e.NewValues["address"] == null ? ipmodel.department : e.NewValues["address"].ToString();
                    ipmodel.description = "监控";
                    ipmodel.usedinternet = 1;
                    Model.ipaddressModel tmpmodel = new BLL.ipaddressBll().GetModel(int.Parse(new BLL.ipaddressBll().GetList(1, string.Format("ip = '{0}'", ipmodel.ip), "id").Tables[0].Rows[0]["id"].ToString()));
                    ipmodel.id = tmpmodel.id;
                    new BLL.ipaddressBll().Update(ipmodel);
                }
            }

        }

        /*
        protected void ASPxGridView1_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e)
        {
            int id = e.VisibleIndex;
            string ip = monitorbll.GetModel(id).ip == null? "":monitorbll.GetModel(id).ip;
            if (ip.Length == 0)
            {
                Maticsoft.Common.MessageBox.Show(this, "非法IP!");
            }
            else
            {
                Response.Write(string.Format("<script language='javascript'>window.open('http://{0}')</script>",ip));
            }

        }
        */

        protected void ASPxGridView1_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {

        }

        protected void ASPxButton5_Click(object sender, EventArgs e)
        {
            List<object> selected = ASPxGridView1.GetSelectedFieldValues("id");
            if (selected.Count != 1)
            {
                Maticsoft.Common.MessageBox.Show(this, "只能选择一个");
            }
            else
            {
                string ip = monitorbll.GetModel(int.Parse(selected[0].ToString())).ip == null ? "" : monitorbll.GetModel(int.Parse(selected[0].ToString())).ip;
                if (ip.Length == 0)
                {
                    Maticsoft.Common.MessageBox.Show(this, "非法IP!");
                }
                else
                {
                    Response.Write(string.Format("<script language='javascript'>window.open('http://{0}');</script>", ip));
                }
            }
        }

        protected void ASPxButton3_Click(object sender, EventArgs e)
        {
            //选择的加入到tmppinglist
            List<object> selectedlist = ASPxGridView1.GetSelectedFieldValues("id");

            if (selectedlist.Count == 0)
            {
                Maticsoft.Common.MessageBox.Show(this, "未选中一行数据!");
            }
            else
            {
                foreach (int select in selectedlist)
                {
                    //把选择的加入到tmppinglist中
                    new BLL.tmppinglistBll().AddSelected(select, Session["username"].ToString());
                }
                Maticsoft.Common.MessageBox.Show(this, "添加成功!");
            }
        }

        protected void ASPxButton4_Click(object sender, EventArgs e)
        {
            List<Model.monitorModel> monitorlist = monitorbll.GetModelList("id > 0");
            foreach (Model.monitorModel monitormodel in monitorlist)
            {
                new BLL.tmppinglistBll().AddSelected(monitormodel.id, Session["username"].ToString());
            }
            Maticsoft.Common.MessageBox.Show(this, "添加成功!");
        }

        protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            string ip = e.Values["ip"].ToString();
            int id = int.Parse(new BLL.ipaddressBll().GetList(1, string.Format("ip = '{0}'", ip), "id").Tables[0].Rows[0]["id"].ToString());
            Model.ipaddressModel ipmodel = new BLL.ipaddressBll().GetModel(id);
            ipmodel.department = "";
            ipmodel.macaddress = "";
            ipmodel.modifydate = DateTime.Now;
            ipmodel.organization = "";
            ipmodel.usedinternet = 1;
            ipmodel.usedperson = "";
            ipmodel.description = "";
            new BLL.ipaddressBll().Update(ipmodel);
        }
    }
}